<?php
/*
 * 基本配置操作 -友链-本地静态缓存数据
 * @auth suosuo
 */
 namespace Common\Model;
 use Think\Model;
 
 class LinksModel extends  Model {
 	private $_db ='';
	public function __construct(){
		$this->_db = M('links');
	}
	
	public function insert($data = array()){
		if(!$data || !is_array($data)){
			return 0;
		}
		$data['create_time'] = time();
		return $this->_db->add($data);
	}
	
	public function getLinks($data,$page,$pageSize=10) {
		$data['status']=array('neq',-1);
		$offset = ($page-1)*$pageSize;
		$list = $this->_db->where($data)->order('id desc')->limit($offset,$pageSize)->select();
	    return $list;
	}
	
	//前端获取友链默认50条
	public function linksForHome () {
		$data['status']=array('neq',-1);
		$list = $this->_db->where($data)->order('id desc')->limit(0,50)->select();
	    return $list;
	}
	
	public function getLinksCount($data=array()){
		$data['status']=array('neq',-1);
		return $this->_db->where($data)->count();
	}
	public function find ($id)
	{
		if(!$id || !is_numeric($id)){
			return array();
		}
		return $this->_db->where('menu_id='.$id)->find();
	}
	
	public function updataLinksById($id,$data){
		if(!$id || !is_numeric($id)){
			throw_exception('ID不合法');
		}
		if(!$data || !is_array($data)){
			throw_exception('更新的数据不合法');
		}
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function updataStatusById($id,$status){
		if(!is_numeric($id) || !$id){
			throw_exception("ID不合法");
		}
		if(!is_numeric($status) || !$status){
			throw_exception("状态不合法");
		}
		$data['status']=$status;
		return $this->_db->where('id='.$id)->save($data);
	}
	
	public function save ($data = array ()){
		if(!$data){
			throw_exception("没有提交的数据");
		}
		$id = F('links_web_config',$data);
		return $id;
	}
	
	public function select () {
		return F('links_web_config');
	}
 }